﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CP.BE;
using CP.BL;

namespace CP.UI
{
    public partial class CRUDCliente : Form
    {
        public ICLIENTEBL clienteBL { get; set; }
        public CRUDCliente()
        {
            InitializeComponent();
            clienteBL = new CLIENTEBL();
            dgvCliente.DataSource = clienteBL.Listadeclientes();
        }

        private void btnRegistrar_Click(object sender, EventArgs e)
        {
            bool sexo;
            if (rbM.Checked == true )
            {
                sexo = false;
            }
            else
            {
                sexo = true;
            }

            clienteBL.RegistarCliente(new CLIENTEBE(txtNombres.Text, txtApellidos.Text, mtxtDNI.Text, dtpFecha.Value.Date, sexo));
            dgvCliente.DataSource = clienteBL.Listadeclientes();

            txtCodigo.Text = "";
            txtNombres.Text = "";
            txtApellidos.Text = "";
            mtxtDNI.Text = "";
        }

        private void dgvCliente_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgvCliente.Rows[e.RowIndex];
            row.Selected = true;
            txtCodigo.Text = row.Cells[0].Value.ToString();
            txtNombres.Text = row.Cells[1].Value.ToString();
            txtApellidos.Text = row.Cells[2].Value.ToString();
            mtxtDNI.Text = row.Cells[3].Value.ToString();
            dtpFecha.Value = DateTime.Parse(row.Cells[4].Value.ToString());
            if (row.Cells[5].Value.ToString() == "Masculino")
            {
                rbM.Checked = true;
                rbF.Checked = false;
            }
            else
            {

                rbM.Checked = false;
                rbF.Checked = true;

            }
            
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
            bool sexo;

            if (rbM.Checked == true)
            {
                sexo = false;
            }
            else
            {
                sexo = true;
            }


            clienteBL.UpdateCliente(new CLIENTEBE(Int32.Parse(txtCodigo.Text), txtNombres.Text, txtApellidos.Text, mtxtDNI.Text, dtpFecha.Value.Date, sexo));
            dgvCliente.DataSource = clienteBL.Listadeclientes();

            txtCodigo.Text = "";
            txtNombres.Text = "";
            txtApellidos.Text = "";
            mtxtDNI.Text = "";
        }

        private void btnELiminar_Click(object sender, EventArgs e)
        {
            clienteBL.DeleteCliente(new CLIENTEBE(Int32.Parse(txtCodigo.Text)));
            dgvCliente.DataSource = clienteBL.Listadeclientes();
            
        }

       

        
       
    }
}
